﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_Public;
using QLT_BLL;

namespace GiaoDien
{
    public partial class frm_HoaDonBH_KH_Them : DevExpress.XtraEditors.XtraForm
    {
        public frm_HoaDonBH_KH_Them()
        {
            InitializeComponent();
        }
        private int _Flag;

        public int Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }
        private int _MaKH;

        public int MaKH
        {
            get { return _MaKH; }
            set { _MaKH = value; }
        }
        private string _TenKH;

        public string TenKH
        {
            get { return _TenKH; }
            set { _TenKH = value; }
        }
        private string _DiaChi;

        public string DiaChi
        {
            get { return _DiaChi; }
            set { _DiaChi = value; }
        }
        public void GetData()
        {
            txtMaKH.Text = MaKH.ToString();
            txtTenKH.Text = TenKH;
            txtDiaChi.Text = DiaChi;
        }
        public void GetLoad()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Mới Khách Hàng";
            }
            if (Flag == 1)
            {
                txtMaKH.Enabled = true;
                GetData();
                this.Text = "Sửa Thông Tin Khách Hàng";
            }
        }
        KhachHang_BLL khachHang_bll = new KhachHang_BLL();
        private void frm_HoaDonBH_KH_Them_Load(object sender, EventArgs e)
        {
            GetLoad();
        }
        public delegate void GetInt(int load);
        public GetInt MyGetLoad;
        private void btnLuu_Click(object sender, EventArgs e)
        {
            KhachHang_Public kh = new KhachHang_Public();
            if (txtTenKH.Text != "" && txtDiaChi.Text != "")
            {
                if (Flag == 0)
                {
                    //thêm
                    kh.TenKH = txtTenKH.Text;
                    kh.DiaChi = txtDiaChi.Text;
                    khachHang_bll.insert_KhachHang(kh, kh);
                    XtraMessageBox.Show("Thêm mới khách hàng thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                if (Flag == 1)
                {
                    //Sửa
                    kh.MaKH = int.Parse(txtMaKH.Text);
                    kh.TenKH = txtTenKH.Text;
                    kh.DiaChi = txtDiaChi.Text;
                    khachHang_bll.update_KhachHang(kh, kh, kh);
                    XtraMessageBox.Show("Đã sửa khách hàng thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            btnHuy_Click(sender,e);
            MyGetLoad(1);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            txtMaKH.ResetText();
            txtTenKH.ResetText();
            txtDiaChi.ResetText();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtTenKH_Leave(object sender, EventArgs e)
        {
            if (txtTenKH.Text == "")
                txtTenKH.Focus();
        }

        private void txtDiaChi_Leave(object sender, EventArgs e)
        {
            if (txtDiaChi.Text == "")
                txtDiaChi.Focus();
        }

    }
}